package org.jeecg.modules.sanitation2.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {
    public static final String hhmmFormat = "HH:mm";
    public static final String MMddFormat = "MM-dd";
    public static final String yyyyFormat = "yyyy";
    public static final String yyyyChineseFormat = "yyyy年";
    public static final String yyyyMMddFormat = "yyyy-MM-dd";
    public static final String yyyyMMdd = "yyyyMMdd";
    public static final String fullFormat = "yyyy-MM-dd HH:mm:ss";
    public static final String payfullFormat = "yyyyMMddHHmmss";
    public static final String MMddChineseFormat = "MM月dd日";
    public static final String yyyyMMddChineseFormat = "yyyy年MM月dd日";
    public static final String fullChineseFormat = "yyyy年MM月dd日 HH时mm分ss秒";
    public static final String[] WEEKS = { "星期日", "星期一", "星期二", "星期三", "星期四",
            "星期五", "星期六" };

    /**
     * 得到指定时间的时间日期格式
     *
     * @param date
     *            指定的时间
     * @param format
     *            时间日期格式
     * @return
     */
    public static String getFormatDateTime(Date date, String format) {
        DateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }

    // 返回当前年份
    public static int getYear() {
        Date date = new Date();
        String year = new SimpleDateFormat("yyyy").format(date);
        return Integer.parseInt(year);
    }

    // 返回当前月份
    public static int getMonth() {
        Date date = new Date();
        String month = new SimpleDateFormat("MM").format(date);
        return Integer.parseInt(month);
    }

    // 返回当前几号
    public static int getDay() {
        Date date = new Date();
        String day = new SimpleDateFormat("d").format(date);
        return Integer.parseInt(day);
    }

    /**
     * 得到指定月的天数
     * */
    public static int getMonthLastDay() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, getYear());
        a.set(Calendar.MONTH, getMonth() - 1);
        a.set(Calendar.DATE, 1);// 把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);// 日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 得到指定月的天数
     * */
    public static int lastDays() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, getYear());
        a.set(Calendar.MONTH, getMonth() - 1);
        a.set(Calendar.DATE, 1);// 把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);// 日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 判断是否是润年
     *
     * @param date
     *            指定的时间
     * @return true:是润年,false:不是润年
     */
    public static boolean isLeapYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return isLeapYear(cal.get(Calendar.YEAR));
    }

    /**
     * 判断是否是润年
     *
     * @param
     *            指定的年
     * @return true:是润年,false:不是润年
     */
    public static boolean isLeapYear(int year) {
        GregorianCalendar calendar = new GregorianCalendar();
        return calendar.isLeapYear(year);
    }

    /**
     * 判断指定的时间是否是今天
     *
     * @param date
     *            指定的时间
     * @return true:是今天,false:非今天
     */
    public static boolean isInToday(Date date) {
        boolean flag = false;
        Date now = new Date();
        String fullFormat = getFormatDateTime(now, DateUtil.yyyyMMddFormat);
        String beginString = fullFormat + " 00:00:00";
        String endString = fullFormat + " 23:59:59";
        DateFormat df = new SimpleDateFormat(DateUtil.fullFormat);
        try {
            Date beginTime = df.parse(beginString);
            Date endTime = df.parse(endString);
            flag = date.before(endTime) && date.after(beginTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 判断两时间是否是同一天
     *
     * @param from
     *            第一个时间点
     * @param to
     *            第二个时间点
     * @return true:是同一天,false:非同一天
     */
    public static boolean isSameDay(Date from, Date to) {
        boolean isSameDay = false;
        DateFormat df = new SimpleDateFormat(DateUtil.yyyyMMddFormat);
        String firstDate = df.format(from);
        String secondDate = df.format(to);
        isSameDay = firstDate.equals(secondDate);
        return isSameDay;
    }

    /**
     * 求出指定的时间那天是星期几
     *
     * @param date
     *            指定的时间
     * @return 星期X
     */
    public static String getWeekString(Date date) {
        return DateUtil.WEEKS[getWeek(date) - 1];
    }

    /**
     * 求出指定时间那天是星期几
     *
     * @param date
     *            指定的时间
     * @return 1-7
     */
    public static int getWeek(Date date) {
        int week = 0;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        week = cal.get(Calendar.DAY_OF_WEEK);
        return week;
    }

    /**
     * 取得指定时间离现在是多少时间以前，如：3秒前,2小时前等 注意：此计算方法不是精确的
     *
     * @param date
     *            已有的指定时间
     * @return 时间段描述
     */
    public static String getAgoTimeString(Date date) {
        Date now = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        Date agoTime = cal.getTime();
        long mtime = now.getTime() - agoTime.getTime();
        String str = "";
        long stime = mtime / 1000;
        long minute = 60;
        long hour = 60 * 60;
        long day = 24 * 60 * 60;
        long weeks = 7 * 24 * 60 * 60;
        long months = 100 * 24 * 60 * 60;
        if (stime < minute) {
            long time_value = stime;
            if (time_value <= 0) {
                time_value = 1;
            }
            str = time_value + "秒前";
        } else if (stime >= minute && stime < hour) {
            long time_value = stime / minute;
            if (time_value <= 0) {
                time_value = 1;
            }
            str = time_value + "分前";
        } else if (stime >= hour && stime < day) {
            long time_value = stime / hour;
            if (time_value <= 0) {
                time_value = 1;
            }
            str = time_value + "小时前";
        } else if (stime >= day && stime < weeks) {
            long time_value = stime / day;
            if (time_value <= 0) {
                time_value = 1;
            }
            str = time_value + "天前";
        } else if (stime >= weeks && stime < months) {
            DateFormat df = new SimpleDateFormat(DateUtil.MMddFormat);
            str = df.format(date);
        } else {
            DateFormat df = new SimpleDateFormat(DateUtil.yyyyMMddFormat);
            str = df.format(date);
        }
        return str;
    }

    /**
     * 判断指定时间是否是周末
     *
     * @param date
     *            指定的时间
     * @return true:是周末,false:非周末
     */
    public static boolean isWeeks(Date date) {
        boolean isWeek = false;
        isWeek = (getWeek(date) - 1 == 0 || getWeek(date) - 1 == 6);
        return isWeek;
    }

    /**
     * 得到今天的最开始时间
     *
     * @return 今天的最开始时间
     */
    public static Date getTodayBeginTime() {
        String beginString = DateUtil.yyyyMMddFormat + " 00:00:00";
        DateFormat df = new SimpleDateFormat(DateUtil.fullFormat);
        Date beginTime = new Date();
        try {
            beginTime = df.parse(beginString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return beginTime;
    }

    /**
     * 得到今天的最后结束时间
     *
     * @return 今天的最后时间
     */
    public static Date getTodayEndTime() {
        String endString = DateUtil.yyyyMMddFormat + " 23:59:59";
        DateFormat df = new SimpleDateFormat(DateUtil.fullFormat);
        Date endTime = new Date();
        try {
            endTime = df.parse(endString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return endTime;
    }

    /**
     * 取得本周的开始时间
     *
     * @return 本周的开始时间
     */
    public static Date getThisWeekBeginTime() {
        Date beginTime = null;
        Calendar cal = Calendar.getInstance();
        int week = getWeek(cal.getTime());
        week = week - 1;
        int days = 0;
        if (week == 0) {
            days = 6;
        } else {
            days = week - 1;
        }
        cal.add(Calendar.DAY_OF_MONTH, -days);
        beginTime = cal.getTime();
        return beginTime;
    }

    /**
     * 取得本周的开始日期
     *
     * @param format
     *            时间的格式
     * @return 指定格式的本周最开始时间
     */
    public static String getThisWeekBeginTimeString(String format) {
        DateFormat df = new SimpleDateFormat(format);
        return df.format(getThisWeekBeginTime());
    }

    /**
     * 取得本周的结束时间
     *
     * @return 本周的结束时间
     */
    public static Date getThisWeekEndTime() {
        Date endTime = null;
        Calendar cal = Calendar.getInstance();
        int week = getWeek(cal.getTime());
        week = week - 1;
        int days = 0;
        if (week != 0) {
            days = 7 - week;
        }
        cal.add(Calendar.DAY_OF_MONTH, days);
        endTime = cal.getTime();
        return endTime;
    }

    /**
     * 取得本周的结束日期
     *
     * @param format
     *            时间的格式
     * @return 指定格式的本周结束时间
     */
    public static String getThisWeekEndTimeString(String format) {
        DateFormat df = new SimpleDateFormat(format);
        return df.format(getThisWeekEndTime());
    }

    /**
     * 取得两时间相差的天数
     *
     * @param from
     *            第一个时间
     * @param to
     *            第二个时间
     * @return 相差的天数
     */
    public static long getBetweenDays(Date from, Date to) {
        long days = 0;
        long dayTime = 24 * 60 * 60 * 1000;
        long fromTime = from.getTime();
        long toTime = to.getTime();
        long times = Math.abs(fromTime - toTime);
        days = times / dayTime;
        return days;
    }

    /**
     * 取得两时间相差的小时数
     *
     * @param from
     *            第一个时间
     * @param to
     *            第二个时间
     * @return 相差的小时数
     */
    public static long getBetweenHours(Date from, Date to) {
        long hours = 0;
        long hourTime = 60 * 60 * 1000;
        long fromTime = from.getTime();
        long toTime = to.getTime();
        long times = Math.abs(fromTime - toTime);
        hours = times / hourTime;
        return hours;
    }

    /**
     * 取得在指定时间上加减days天后的时间
     *
     * @param date
     *            指定的时间
     * @param days
     *            天数,正为加，负为减
     * @return 在指定时间上加减days天后的时间
     */
    public static Date addDays(Date date, int days) {
        Date time = null;
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, days);
        time = cal.getTime();
        return time;
    }

    /**
     * 取得在指定时间上加减months月后的时间
     *
     * @param date
     *            指定时间
     * @param months
     *            月数，正为加，负为减
     * @return 在指定时间上加减months月后的时间
     */
    public static Date addMonths(Date date, int months) {
        Date time = null;
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, months);
        time = cal.getTime();
        return time;
    }

    /**
     * 取得在指定时间上加减years年后的时间
     *
     * @param date
     *            指定时间
     * @param years
     *            年数，正为加，负为减
     * @return 在指定时间上加减years年后的时间
     */
    public static Date addYears(Date date, int years) {
        Date time = null;
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, years);
        time = cal.getTime();
        return time;
    }

    public static int getWeekByDate(Date date) {

        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(date);
        int x = aCalendar.get(Calendar.DAY_OF_WEEK);
        int y = 0;
        if (x == 1) {
            y = 7;
        } else if (x == 2) {
            y = 1;
        } else if (x == 3) {
            y = 2;
        } else if (x == 4) {
            y = 3;
        } else if (x == 5) {
            y = 4;
        } else if (x == 6) {
            y = 5;
        } else if (x == 7) {
            y = 6;
        } else {
            y = -1;
        }
        return y;
    }

    public static Date getNextMonthDate(Calendar calendar) {
        // 当前月份
        int currentMonth = calendar.get(Calendar.MONTH);
        // 设置为下一个月
        calendar.set(Calendar.MONTH, currentMonth + 1);
        // 获取下个月的时间
        Date date = calendar.getTime();
        return date;
    }

    public int getDaysBetween(java.util.Calendar d1, java.util.Calendar d2) {
        if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
            java.util.Calendar swap = d1;
            d1 = d2;
            d2 = swap;
        }
        int days = d2.get(java.util.Calendar.DAY_OF_YEAR)
                - d1.get(java.util.Calendar.DAY_OF_YEAR);
        int y2 = d2.get(java.util.Calendar.YEAR);
        if (d1.get(java.util.Calendar.YEAR) != y2) {
            d1 = (java.util.Calendar) d1.clone();
            do {
                days += d1.getActualMaximum(java.util.Calendar.DAY_OF_YEAR);
                d1.add(java.util.Calendar.YEAR, 1);
            } while (d1.get(java.util.Calendar.YEAR) != y2);
        }
        return days;
    }

    /**
     * 计算2个日期之间的相隔天数
     *
     * @param d1
     * @param d2
     * @return
     */
    public int getWorkingDay(java.util.Calendar d1, java.util.Calendar d2) {
        int result = -1;
        if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
            java.util.Calendar swap = d1;
            d1 = d2;
            d2 = swap;
        }
        int betweendays = getDaysBetween(d1, d2);
        int charge_date = 0;
        int charge_start_date = 0;// 开始日期的日期偏移量
        int charge_end_date = 0;// 结束日期的日期偏移量
        // 日期不在同一个日期内
        int stmp;
        int etmp;
        stmp = 7 - d1.get(Calendar.DAY_OF_WEEK);
        etmp = 7 - d2.get(Calendar.DAY_OF_WEEK);
        if (stmp != 0 && stmp != 6) {// 开始日期为星期六和星期日时偏移量为0
            charge_start_date = stmp - 1;
        }
        if (etmp != 0 && etmp != 6) {// 结束日期为星期六和星期日时偏移量为0
            charge_end_date = etmp - 1;
        }
        // }
        result = (getDaysBetween(this.getNextMonday(d1), this.getNextMonday(d2)) / 7)
                * 5 + charge_start_date - charge_end_date;
        return result;
    }

    public String getChineseWeek(Calendar date) {
        final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
                "星期六" };

        int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);

        return dayNames[dayOfWeek - 1];

    }

    /**
     * 获得日期的下一个星期一的日期
     *
     * @param date
     * @return
     */
    public Calendar getNextMonday(Calendar date) {
        Calendar result = null;
        result = date;
        do {
            result = (Calendar) result.clone();
            result.add(Calendar.DATE, 1);
        } while (result.get(Calendar.DAY_OF_WEEK) != 2);
        return result;
    }

    /**
     *
     * @param d1
     * @param d2
     * @return
     */
    public int getHolidays(Calendar d1, Calendar d2) {
        return this.getDaysBetween(d1, d2) - this.getWorkingDay(d1, d2);

    }

    /**
     * 将字符串转换成Date
     *
     * @param date
     * @param format
     * @return
     */
    public static Date strToDate(String date, String format) {
        Date retValue = null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            retValue = sdf.parse(date);
        } catch (ParseException ex) {
            return retValue;
        }
        return retValue;
    }

    public static void main(String[] args) {
        System.out.println(getDay());
    }
}
